package com.example.demo.controller;


import com.example.demo.utils.FilesUpload;
import com.example.demo.utils.LayuiResult;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.HashMap;
import java.util.Map;

@Controller
public class CommonController {




    @RequestMapping("/upload")
    @ResponseBody
    public Object upload(MultipartFile file) throws Exception {
        File indexFile = new File("");
        String path = indexFile.getCanonicalPath() + "\\src\\main\\webapp\\images";
        String fileName = new SimpleDateFormat("yyyyMMddHHmmssssss").format(Calendar.getInstance().getTime());
        String s = FilesUpload.uploadFile(file, path, fileName, "/images/");

        return LayuiResult.LayuiData(0,"上传成功",s);
    }

    @RequestMapping("/uploadLayEdit")
    @ResponseBody
    public Object uploadLayEdit(MultipartFile file) throws Exception {
        File indexFile = new File("");
        String path = indexFile.getCanonicalPath() + "\\src\\main\\webapp\\layedit";
        String fileName = new SimpleDateFormat("yyyyMMddHHmmssssss").format(Calendar.getInstance().getTime());
        String s = FilesUpload.uploadFile(file, path, fileName, "/layedit/");
        Map<String,Object> map = new HashMap<>();
        map.put("src",s);
        return LayuiResult.LayuiData(0,"上传成功",map);
    }

    @RequestMapping("/uploadCases")
    @ResponseBody
    public Object uploadCases(MultipartFile file) throws Exception {
        File indexFile = new File("");
        String path = indexFile.getCanonicalPath() + "\\src\\main\\webapp\\cases";
        String fileName = new SimpleDateFormat("yyyyMMddHHmmssssss").format(Calendar.getInstance().getTime());
        String s = FilesUpload.uploadFile(file, path, fileName, "/cases/");
        return LayuiResult.LayuiData(0,"上传成功",s);
    }

}
